<template>
	<view style="background: #F7F7F7;height: 100vh;">
		<view class="id-container">
			<view class="top-header">
				<view class="t0">上传身份证照片</view>
				<view class="t1">温馨提示：请您拍摄并上传本人真实有效的身份证原件照片，保证照片完整、清晰。</view>
			</view>
			<view class="id-box" v-if="!isAuth">
				<view class="t0">
					上传身份证件照
					<text>(如有误或上传失败,请重新拍照)</text>
				</view>
				<view class="t1">
					<view class="t1-flex">
						<view class="t1-flex-img">
							<image src="../../static/images/account/编组 11@2x.png" mode="" class="bg-img"></image>
							<image src="../../static/images/account/camera.png" mode="" class="camera"></image>
						</view>
						<view class="t1-flex-text">身份证人像面</view>
					</view>
					<view class="t1-flex">
						<view class="t1-flex-img">
							<image src="../../static/images/account/矩形 2@2x.png" mode="" class="bg-img"></image>
							<image src="../../static/images/account/camera.png" mode="" class="camera"></image>
						</view>
						<view class="t1-flex-text">身份证国徽面</view>
					</view>
				</view>
				<view class="remark-tip">
					备注：商户身份信息是应国家法律法规及监管规定要求上传仅用于开通收款及相关服务。
				</view>
			</view>
			<view class="id-box" v-if="isAuth">
				<view class="t0">
					上传身份证件照
					<text>(如有误或上传失败,请重新拍照)</text>
				</view>
				<view class="t1">
					<view class="t1-flex">
						<view class="t1-flex-img" @click="chooseCard(1)">
							<!-- <image src="../../static/images/account/编组 11@2x.png" mode="" class="bg-img"></image> -->
							<image :src="formData.card_z" mode="" class="bg-img"></image>
							<image src="../../static/images/account/camera.png" mode="" class="camera"></image>
						</view>
						<view class="t1-flex-text">身份证人像面</view>
					</view>
					<view class="t1-flex">
						<view class="t1-flex-img" @click="chooseCard(2)">
							<!-- <image src="../../static/images/account/矩形 2@2x.png" mode="" class="bg-img"></image> -->
							<image :src="formData.card_f" mode="" class="bg-img"></image>
							<image src="../../static/images/account/camera.png" mode="" class="camera"></image>
						</view>
						<view class="t1-flex-text">身份证国徽面</view>
					</view>
				</view>
				<uni-forms :modelValue="formData" label-width="90px">
					<uni-forms-item label="姓名" name="name">
						<!-- <uni-easyinput type="text" v-model="formData.realname" /> -->
						<text>{{formData.realname}}</text>
					</uni-forms-item>
					<uni-forms-item label="身份证号码" name="id">
						<!-- <uni-easyinput type="text" v-model="formData.card_no" /> -->
						<text>{{formData.card_no}}</text>
					</uni-forms-item>
					<uni-forms-item required name="validate" label="有效期">
						<view class="form-item">
							<!-- <uni-easyinput v-model="formData.card_start" placeholder="开始时间" /> -->
							<text class="txt">{{formData.card_start}}</text>
							至
							<!-- <uni-easyinput v-model="formData.card_end" placeholder="结束时间" /> -->
							<text>{{formData.card_end}}</text>
						</view>
					</uni-forms-item>
				</uni-forms>
				<view class="remark-tip">
					备注：商户身份信息是应国家法律法规及监管规定要求上传仅用于开通收款及相关服务。
				</view>
			</view>
			<button size="mini" class="btn" :disabled="!isAuth" @click="submit('formData')">提交身份认证</button>
			<view class="privacy-tip">
				我们将会保障您的隐私信息安全，请放心提交。
				<br />
				查看
				<text>《隐私政策》</text>
			</view>
		</view>
	</view>
</template>

<script>
	import api from '@/api';
	import store from '@/store/index.js'
	export default {
		data() {
			return {
				isAuth: true,
				formData: {
					realname: '', //孙雪春
					card_no: '', //410787198998564651
					card_start: '',
					card_end: '',
					card_z: '', // 身份证正面 https://jiedan0512.62.nantaihu1.top/sfzz.jpg
					card_f: '' // 身份证反面 https://jiedan0512.62.nantaihu1.top/sfzz.jpg
				}
			};
		},

		methods: {
			chooseCard(n) {

				uni.chooseImage({
					count: 1,
					sizeType: ['original'],
					sourceType: ['album', 'camera'],
					success: res => {

						if (n == 1) {
							this.uploadImg(res.tempFilePaths[0]).then(img => {
								// 身份证识别

								this.formData.card_z = img.data.fullurl;

								api.idCartInfo({ img: img.data.fullurl, type: 'face' }).then(res => {
									this.formData.realname = res.data.name
									this.formData.card_no = res.data.num;
								})
							})

						} else {

							this.uploadImg(res.tempFilePaths[0]).then(img => {

								this.formData.card_f = img.data.fullurl;

								api.idCartInfo({ img: img.data.fullurl, type: 'back' }).then(res => {
									console.log(res);
									this.formData.card_start = res.data.start_date
									this.formData.card_end = res.data.end_date
								})
							})
						}
					}
				});
			},
			uploadImg(file) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: 'https://d-paopao.bigchun.com/api/common/upload',
						filePath: file,
						header: { token: store.state.token },
						success: img => resolve(JSON.parse(img.data)),
						fail: err => reject(err)
					})
				})
			},
			submit() {
				api.authRealname(this.formData).then(res => {
					if (res.code) {
						uni.showToast({
							title: res.msg
						});
						uni.navigateBack();
					} else {
						uni.showToast({
							title: res.msg
						});
					}
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.id-container {
		background: url('/static/images/home/home-bg.png') no-repeat;
		background-size: 100% 408rpx;
		padding-bottom: 100rpx;
		padding-top: 54rpx;

		.top-header {
			text-align: center;

			.t0 {
				font-family: PingFangSC, PingFang SC;
				font-weight: 600;
				font-size: 44rpx;
				color: #ffffff;
				line-height: 60rpx;
				font-style: normal;
				margin-bottom: 18rpx;
			}

			.t1 {
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 24rpx;
				color: #ffffff;
				line-height: 34rpx;
				font-style: normal;
				padding: 0 28rpx 20rpx;
			}
		}

		.id-box {
			margin: 0 20rpx;
			background: url('/static/images/home/empty-bg.png') no-repeat;
			background-size: 100% 100%;
			padding: 52rpx 36rpx;

			.t0 {
				background: linear-gradient(270deg, rgba(0, 158, 251, 0) 0%, #0099fa 100%) bottom left no-repeat;
				background-size: 78rpx 10rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 30rpx;
				color: #000000;
				line-height: 42rpx;
				font-style: normal;

				uni-text {
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 26rpx;
					color: #b0b0b0;
					line-height: 36rpx;
					font-style: normal;
					margin-left: 12rpx;
				}
			}

			.t1 {
				margin-top: 32rpx;
				display: flex;

				.t1-flex {
					flex: 1;

					.t1-flex-img {
						position: relative;

						.bg-img {
							width: 300rpx;
							height: 188rpx;
						}

						.camera {
							width: 48rpx;
							height: 48rpx;
							position: absolute;
							top: calc(50% - 24rpx);
							left: calc(50% - 24rpx);
						}
					}

					.t1-flex-text {
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 28rpx;
						color: #000000;
						line-height: 40rpx;
						font-style: normal;
						text-align: center;
						margin: 16rpx 0 24rpx;
					}
				}
			}

			.remark-tip {
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 26rpx;
				color: #fb2525;
				line-height: 36rpx;
				font-style: normal;
			}

			.uni-forms {
				.uni-forms-item__label {
					font-size: 28rpx !important;
					color: #000000 !important;
				}

				.uni-forms-item__content {
					.uni-easyinput__content-input {
						border: none !important;
						background: none !important;
					}
				}
			}
		}

		.btn {
			background: linear-gradient(311deg, #93a4fb 0%, #2f9afe 100%);
			box-shadow: 0rpx 4rpx 12rpx 0rpx rgba(20, 135, 255, 0.4);
			border-radius: 22px;
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 34rpx;
			color: #ffffff;
			line-height: 88rpx !important;
			text-align: right;
			font-style: normal;
			height: 88rpx;
			margin: 36rpx 28rpx;
			width: calc(100% - 56rpx);
			text-align: center;
			border: none;

			&:after {
				border: none;
			}
		}

		.privacy-tip {
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #a7a5a5;
			line-height: 52rpx;
			font-style: normal;
			text-align: center;
			padding: 0 28rpx 32rpx;

			uni-text {
				color: #49a1fd;
			}
		}

		.txt {
			display: inline-block;
			min-width: 100rpx;
		}
	}
</style>